<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '请输入公众号名称'}]">
        <el-input v-model="dataForm.name" placeholder="公众号名称"></el-input>
      </el-form-item>
      <el-form-item label="appid" prop="appid" :rules="[{ required: true, message: '请输入公众号appid'}]">
        <el-input v-model="dataForm.appid" placeholder="公众号appid"></el-input>
      </el-form-item>
      <el-form-item label="secret" prop="secret" :rules="[{ required: true, message: '请输入公众号secret'}]">
        <el-input v-model="dataForm.secret" placeholder="公众号secret"></el-input>
      </el-form-item>
      <el-form-item label="token" prop="token" :rules="[{ required: true, message: '请输入公众号token'}]">
        <el-input v-model="dataForm.token" placeholder="公众号token"></el-input>
      </el-form-item>
      <el-form-item label="aeskey" prop="aeskey" :rules="[{ required: true, message: '请输入公众号aeskey'}]">
        <el-input v-model="dataForm.aeskey" placeholder="公众号aeskey"></el-input>
      </el-form-item>
	  <el-form-item label="mchid" prop="mchid" :rules="[{ required: true, message: '请输入公众号mchid'}]">
        <el-input v-model="dataForm.mchid" placeholder="公众号mchid"></el-input>
      </el-form-item>
	  <el-form-item label="mchKey" prop="mchKey" :rules="[{ required: true, message: '请输入公众号mchKey'}]">
        <el-input v-model="dataForm.mchKey" placeholder="公众号mchKey"></el-input>
      </el-form-item>
	  <el-form-item label="证书文件全路径名" prop="keyPath" :rules="[{ required: true, message: '请输入公众号p12证书文件的全路径名'}]">
        <el-input v-model="dataForm.keyPath" placeholder="p12证书文件的全路径名"></el-input>
      </el-form-item>
	  
	  
      
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  import { Debounce } from '@/utils/debounce'
  import { encrypt } from '@/utils/crypto'
  export default {
    data () {
      
      return {
        visible: false,
        roleList: [],
		malls: [],
        dataForm: {
          id: 0,
          appid: '',
          secret: '',
          token: '',
          aeskey: '',
          mchid: '',
		  mchKey: '',
		  keyPath: '',
		  name: '',
          roleIdList: [],
          status: 1,
		  mallid: 0
        }
        
      }
    },
	
    methods: {
	  
		
      init (id) {
		this.dataForm.id = id || 0
		if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/mp/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.dataForm.appid = data.appid
              this.dataForm.secret = data.secret
              this.dataForm.token = data.token
              this.dataForm.aeskey = data.aeskey
              this.dataForm.mchid = data.mchid
			  this.dataForm.mchKey=data.mchKey
			  this.dataForm.keyPath=data.keyPath
			  this.dataForm.name=data.name
            })
        }
	    this.visible = true
          this.$nextTick(() => {
            this.$refs.dataForm.resetFields()
          })
        
        
      },
      // 表单提交
      dataFormSubmit: Debounce(function () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
		    console.log(this.dataForm.password,encrypt(this.dataForm.password))
            this.$http({
              url: this.$http.adornUrl(`/sys/mp`),
              method: this.dataForm.id ? 'put' : 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'appid': this.dataForm.appid,
                'secret': this.dataForm.secret,
                'token': this.dataForm.token,
                'aeskey': this.dataForm.aeskey,
                'mchid': this.dataForm.mchid,
                'mchKey': this.dataForm.mchKey,
				'name': this.dataForm.name,
				'keyPath': this.dataForm.keyPath
              })
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        })
      })
    }
  }
</script>
